home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / dev / gcc / gcc270cp.lha / gnu / lib / g++-include / stack.h < prev    next >
C/C++ Source or Header  |  1995-07-28  |  4KB  |  119 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  */
  15.  
  16. #ifndef STACK_H
  17. #define STACK_H
  18.  
  19. #include <bool.h>
  20. #include <heap.h>
  21.  
  22. template <class Container>
  23. class stack {
  24. friend bool operator==(const stack<Container>& x, const stack<Container>& y);
  25. friend bool operator<(const stack<Container>& x, const stack<Container>& y);
  26. public:
  27.     typedef Container::value_type value_type;
  28.     typedef Container::size_type size_type;
  29. protected:
  30.     Container c;
  31. public:
  32.     bool empty() const { return c.empty(); }
  33.     size_type size() const { return c.size(); }
  34.     value_type& top() { return c.back(); }
  35.     const value_type& top() const { return c.back(); }
  36.     void push(const value_type& x) { c.push_back(x); }
  37.     void pop() { c.pop_back(); }
  38. };
  39.  
  40. template <class Container>
  41. bool operator==(const stack<Container>& x, const stack<Container>& y) {
  42.     return x.c == y.c;
  43. }
  44.  
  45. template <class Container>
  46. bool operator<(const stack<Container>& x, const stack<Container>& y) {
  47.     return x.c < y.c;
  48. }
  49.  
  50. template <class Container>
  51. class queue {
  52. friend bool operator==(const queue<Container>& x, const queue<Container>& y);
  53. friend bool operator<(const queue<Container>& x, const queue<Container>& y);
  54. public:
  55.     typedef Container::value_type value_type;
  56.     typedef Container::size_type size_type;
  57. protected:
  58.     Container c;
  59. public:
  60.     bool empty() const { return c.empty(); }
  61.     size_type size() const { return c.size(); }
  62.     value_type& front() { return c.front(); }
  63.     const value_type& front() const { return c.front(); }
  64.     value_type& back() { return c.back(); }
  65.     const value_type& back() const { return c.back(); }
  66.     void push(const value_type& x) { c.push_back(x); }
  67.     void pop() { c.pop_front(); }
  68. };
  69.  
  70. template <class Container>
  71. bool operator==(const queue<Container>& x, const queue<Container>& y) {
  72.     return x.c == y.c;
  73. }
  74.  
  75. template <class Container>
  76. bool operator<(const queue<Container>& x, const queue<Container>& y) {
  77.     return x.c < y.c;
  78. }
  79.  
  80. template <class Container, class Compare> 
  81. // Compare = less<Container::value_type> >
  82. class  priority_queue {
  83. public:
  84.     typedef Container::value_type value_type;
  85.     typedef Container::size_type size_type;
  86. protected:
  87.     Container c;
  88.     Compare comp;
  89. public:
  90.     priority_queue(const Compare& x = Compare()) :  c(), comp(x) {}
  91.     priority_queue(const value_type* first, const value_type* last, 
  92.            const Compare& x = Compare()) : c(first, last), comp(x) {
  93.     make_heap(c.begin(), c.end(), comp);
  94.     }
  95. /*
  96.     template <class InputIterator>
  97.     priority_queue(InputIterator first, InputIterator last, 
  98.            const Compare& x = Compare()) : c(first, last), comp(x) {
  99.     make_heap(c.begin(), c.end(), comp);
  100.     }
  101. */
  102.     bool empty() const { return c.empty(); }
  103.     size_type size() const { return c.size(); }
  104.     value_type& top() { return c.front(); }
  105.     const value_type& top() const { return c.front(); }
  106.     void push(const value_type& x) { 
  107.     c.push_back(x); 
  108.     push_heap(c.begin(), c.end(), comp);
  109.     }
  110.     void pop() { 
  111.     pop_heap(c.begin(), c.end(), comp);
  112.     c.pop_back(); 
  113.     }
  114. };
  115.  
  116. // no equality is provided
  117.  
  118. #endif
  119.